//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if _MSC_VER > 1000
#pragma once
#endif

#ifndef __PSEUDO_H__
#define __PSEUDO_H__

#include <elasys_server.h>
#include <clsinfo.h>

_ELASTOS_NAMESPACE_USING

class CPseudoKernel
{
public:
    STATIC CARAPI System_GetTime(
        /* [out] */ SystemTime *pTime);

    STATIC CARAPI System_SetTime(
        /* [in] */ SystemTime * pSTime);

    STATIC CARAPI System_GetElapsedTime(
        /* [out] */ Millisecond32 * pTime);

    STATIC CARAPI System_QueryPerformanceCounter(
        /* [out] */ Int64 * pCounter);
        
    STATIC CARAPI System_QueryPerformanceFrequency(
        /* [out] */ Int64 * pFrequency);        

    STATIC CARAPI System_GetIdleTime(
        /* [out] */ Millisecond32 * pTime);

    STATIC CARAPI System_Reboot();

    STATIC CARAPI System_GetAllProcesses(
        /* [out] */ IObjectEnumerator ** ppProcesses);

    STATIC CARAPI System_GetMemorySizes(
        /* [out] */ Int32 * pTotalPhysicalMemorySize,
        /* [out] */ Int32 * pAvailablePhysicalMemorySize);

    STATIC CARAPI System_GetHostingManager(
        /* [out] */ IHostingManager ** ppHostingManager);

    STATIC CARAPI System_TryWaitForAnyEvents(
        /* [in] */ const BufferOf<IEvent *> * pEvents,
        /* [in] */ Millisecond32 timeout,
        /* [out] */ WaitResult * pResult,
        /* [out] */ Int32 * pIndex,
        /* [out] */ EventState * pState);

    STATIC CARAPI System_RequestIntrestedEvent(
        /* [in] */ SystemEvents intrestedEvents,
        /* [out] */ IEvent ** ppEvent);

    STATIC CARAPI System_GetEnvironmentMutex(
        /* [out] */ IMutex ** ppEnvMutex);

    //------------------------------------------
    STATIC CARAPI Process_CreateWithUnstarted(
        /* [out] */ IProcess ** ppProcess);

    STATIC CARAPI Process_GetCurrent(
        /* [out] */ IProcess ** ppProcess);

    STATIC CARAPI Process_GetCurrentId(
        /* [out] */ ProcessId * pProcessId);

    STATIC CARAPI Process_ExitCurrent(
        /* [in] */ ExitCode exitCode);

    STATIC CARAPI Process_LoadModule(
        /* [in] */ WString name,
        /* [in] */ ModuleLoadFlags flags,
        /* [out] */ IModule ** ppModule);

    STATIC CARAPI Process_FlushInstructionCache(
        /* [in] */ Address basAddr,
        /* [in] */ MemorySize size);

    //------------------------------------------
    STATIC CARAPI Thread_Create(
        /* [in] */ Address threadMainStartup,
        /* [in] */ Address threadRoutine,
        /* [in] */ Address threadArg,
        /* [in] */ ThreadCreationFlags flags,
        /* [out] */ IThread ** ppThread);

    STATIC CARAPI Thread_GetCurrent(
        /* [out] */ IThread ** ppThread);

    STATIC CARAPI Thread_GetCurrentId(
        /* [out] */ ThreadId * pThreadId);

    STATIC CARAPI Thread_CurrentIsMain(
        /* [out] */ Boolean * pMainThread);

    STATIC CARAPI Thread_QuitCurrent(
        /* [in] */ QuitCode quitCode);

    STATIC CARAPI Thread_Sleep(
        /* [in] */ Millisecond32 timeout,
        /* [out] */ WaitResult * pResult);

    STATIC CARAPI Thread_Yield();

    //------------------------------------------
    STATIC CARAPI  VirtualMemory_Map(
        /* [in] */ Address startingAddr,
        /* [in] */ MemorySize size,
        /* [in] */ MemoryMapFlags flags,
        /* [in] */ MemoryProtection protect,
        /* [out] */ Address *pSettledAddr);

    STATIC CARAPI VirtualMemory_Query(
        /* [in] */ Address address,
        /* [out] */ Address *pBaseAddr,
        /* [out] */ MemorySize *pSize);

    STATIC CARAPI VirtualMemory_SetProtection(
        /* [in] */ Address baseAddr,
        /* [in] */ MemorySize size,
        /* [in] */ MemoryProtection protect);

    STATIC CARAPI VirtualMemory_CheckProtection(
        /* [in] */ Address address,
        /* [in] */ MemoryProtection protect,
        /* [out] */ Boolean *pValid);

    STATIC CARAPI VirtualMemory_Commit(
        /* [in] */ Address address);

    STATIC CARAPI VirtualMemory_IoRemap(
        /* [in] */ Address startingAddr,
        /* [in] */ MemorySize size,
        /* [in] */ Address physicalAddr,
        /* [in] */ MemoryMapFlags flags,
        /* [in] */ MemoryProtection protect,
        /* [out] */ Address *pSettledAddr);

    STATIC CARAPI VirtualMemory_Unmap(
        /* [in] */ Address baseAddr,
        /* [in] */ MemorySize size);

    //------------------------------------------
    STATIC CARAPI Mutex_Create(
        /* [out] */ IMutex ** ppMutex);

    STATIC CARAPI Mutex_Create(
        /* [in] */ WString name,
        /* [out] */ IMutex ** ppMutex,
        /* [out] */ Boolean * pAlreadyExisted);

    STATIC CARAPI Event_Create(
        /* [in] */ Boolean manualReset,
        /* [in] */ EventState initialState,
        /* [out] */ IEvent ** ppEvent);

    STATIC CARAPI Event_Create(
        /* [in] */ WString name,
        /* [in] */ Boolean manualReset,
        /* [in] */ EventState initialState,
        /* [out] */ IEvent ** ppEvent,
        /* [out] */ Boolean * pAlreadyExisted);

    STATIC CARAPI Condition_Create(
        /* [out] */ ICondition ** ppCondition);

    STATIC CARAPI Condition_Create(
        /* [in] */ WString name,
        /* [out] */ ICondition ** ppCondition,
        /* [out] */ Boolean * pAlreadyExisted);

    STATIC CARAPI ReaderWriterLock_Create(
        /* [out] */ IReaderWriterLock ** ppRwlock);

    STATIC CARAPI ReaderWriterLock_Create(
        /* [in] */ WString name,
        /* [out] */ IReaderWriterLock ** ppRwlock,
        /* [out] */ Boolean * pAlreadyExisted);

    STATIC CARAPI SharedMemory_Create(
        /* [in] */ WString name,
        /* [in] */ MemorySize size,
        /* [in] */ Boolean commit,
        /* [out] */ ISharedMemory ** ppSharedMemory,
        /* [out] */ Boolean * pAlreadyExisted);

    //------------------------------------------
    STATIC CARAPI Tls_AcquireSlot(
        /* [in] */ ThreadLocalSlotType type,
        /* [out] */ Int32 * pSlotIndex);

    STATIC CARAPI Tls_RelinquishSlot(
        /* [in] */ Int32 slotIndex,
        /* [out] */ BufferOf<Address> * pUsedPtrs);

    STATIC CARAPI Tls_CleanupSlot(
        /* [in] */ Int32 slotIndex,
        /* [out] */ BufferOf<Address> * pUsedPtrs);

    STATIC CARAPI Tls_GetValue(
        /* [in] */ ThreadLocalSlotType type,
        /* [in] */ Int32 slotIndex,
        /* [out] */ PVoid * ppValue);

    STATIC CARAPI Tls_SetValue(
        /* [in] */ ThreadLocalSlotType type,
        /* [in] */ Int32 slotIndex,
        /* [in] */ PVoid pValue);

    //------------------------------------------
    STATIC CARAPI SystemEvent_Create(
        /* [out] */ PVoid * phSysEvent);

    STATIC CARAPI SystemEvent_Wait(
        /* [in] */ PVoid hSysEvent);

    STATIC CARAPI SystemEvent_Notify(
        /* [in] */ PVoid hSysEvent);

    STATIC CARAPI SystemEvent_Destroy(
        /* [in] */ PVoid hSysEvent);
};

enum BreakPointType
{
    BreakPointType_Exec         = 0x01,
    BreakPointType_Write        = 0x02,
    BreakPointType_ReadWrite    = 0x03,
};

class CPseudoSyscall
{
public:

    STATIC CARAPI SysPrint(
        /* [in]  */ const wchar_t * str);

    STATIC CARAPI SysErrorPrint(
        /* [in]  */ const wchar_t * wstr);

    STATIC CARAPI SysGetVirtualCode(
        /* [in]  */ struct KBDStatus * pVirtualCode);

    STATIC CARAPI SysSetBreakPoint(
        /* [in]  */ UInt32 uNo,
        /* [in]  */ BreakPointType type,
        /* [in]  */ PVoid  pvAddress,
        /* [in]  */ UInt32 uLength);

    STATIC CARAPI SysRegisterClassInfo(
        /* [in]  */ CIClassInfo * pClassInfo);

    STATIC CARAPI SysReply(
        /* [in]  */ ECode ec,
        /* [in]  */ PVoid pvOutBuf,
        /* [in]  */ size_t uOutBufSize);

    STATIC CARAPI RegisterRunningObject(
        /* [in]  */ WString name,
        /* [in]  */ PInterface pService);

    STATIC CARAPI FindRunningObject(
        /* [in]  */ WString name,
        /* [in]  */ PInterface * ppService);

    STATIC CARAPI WaitForRunningObject(
        /* [in]  */ WString name,
        /* [in]  */ Int32 timeout,
        /* [out] */ WaitResult *pResult);

    STATIC CARAPI UnregisterRunningObject(
        /* [in]  */ WString name);
};

EXTERN_C ECode CDECL PseudoKernel_Initialize();
EXTERN_C void  CDECL PseudoKernel_Destroy();

EXTERN_C ECode CDECL PseudoKernel_ThreadStartEntry(PVoid pArgs);

#endif // __PSEUDO_H__
